package 数组.最大连续1的个数485;

public class Solution {
    //方法一
    public int findMaxConsecutiveOnes(int[] nums) {
        int[] arr = new int[nums.length];
        int count = 0;
        for(int i = 0; i < nums.length; i++){
            if(nums[i] == 1){
                count += 1;
            }else{
                count = 0;
            }
            arr[i] = count;
        }

        int max_ = -1;
        for(int i = 0; i < arr.length; i++){
            if(arr[i] > max_){
                max_ = arr[i];
            }
        }

        return max_;
    }

    //方法二
    public int findMaxConsecutiveOnes2(int[] nums) {
        int maxCount = 0, count = 0;
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            if (nums[i] == 1) {
                count++;
            } else {
                maxCount = Math.max(maxCount, count);
                count = 0;
            }
        }
        maxCount = Math.max(maxCount, count);
        return maxCount;
    }
}
